import { Component, OnInit } from '@angular/core';

import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { FormBuilder, Validators, FormControl } from "@angular/forms";
import { Command } from "../../../../shared/toolbar.service";
import { Observable } from "rxjs/Observable";
import { BladeData } from "../../../../shared/blade/blade-data";

import { MatrixStateGroup } from "../../../../entity/matrix-state-group.model";
import { MatrixStateGroupService } from "../../../../service/matrix-state-group.service";

@Component({
    selector: 'app-matrix-state-group-edit',
    templateUrl: './matrix-state-group-edit.component.html',
    styleUrls: ['./matrix-state-group-edit.component.css']
})
export class MatrixStateGroupEditComponent extends BladeContainerBase<MatrixStateGroup> {

    public matrixStateGroups: MatrixStateGroup[] = [];
    public original: MatrixStateGroup[] = [];

    constructor(
        private fb: FormBuilder,
        private matrixStateGroupService: MatrixStateGroupService) {
        super(fb)
    }

    ngOnInit() {
        //编辑时注册按钮和按钮命令    
        if (!this.isNew) {
            this.commands = [
                new Command("save", "保存", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
                new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
            ]
        }
        else {
            this.currentEntity = new MatrixStateGroup(0, '', true, (this.parentBlade.parentEntity as MatrixStateGroup).tagId);
        }
        super.ngOnInit();
    }

    exeSaveChange(blade: any): void {
        this.saveChange();
    }
    canSaveChange(blade: any): boolean {
        return this.hasChange() && !this.hasError();
    }
    exeReset(blade: any): void {
        return this.formGroup.reset(this.currentEntity);
    }
    canReset(blade: any): boolean {
        return this.hasChange();
    }

	/**
    * 初始化数据
    * 
    * 
    * @memberof BladeContainerBase
    */
    initData(): any | Observable<any> | Promise<any> {
        this.formPatchValue(this.currentEntity);
    }

    /**
    * 创建表单
    * 
    * 
    * @memberof DicKeyEditComponent
    */
    createForm() {
        this.formGroup = this.fromBuider.group({
            "stateGroupName": [this.currentEntity.stateGroupName, [
                Validators.required,
                Validators.maxLength(20),

            ], (control: FormControl): any => this.stateGroupNameAsnyValidator(control)],  //名称的异步校验;
        });

    }

    /**
    * 设置表单错误对象
    * 
    * 
    * @memberof BladeContainerBase
    */
    setFormError(): void {
        this.formErrors = {
            'stateGroupName': '',
        };
    }
    /**
    * 设置表单验证信息
    * 
    * 
    * @memberof BladeContainerBase
    */
    setValidationMessages(): void {
        this.validationMessages = {
            'stateGroupName': {
                'required': '状态组名称不能为空。',
                'maxlength': '名称不能多于20个字符长度。',
                'pattern': '名称由汉字组成。',
                'exist': '名称已存在',
            },
        };
    }

    /**
     * 异步校验Name是否存在
     * 
     * @param {FormControl} control 
     * @returns {Promise<true>} 
     * 
     * @memberof MatrixStateGroupEditComponent
     */
    stateGroupNameAsnyValidator(control: FormControl): any {
        return this.matrixStateGroupService.hasStateGroupName(control.value, this.currentEntity.stateGroupId, this.currentEntity.tagId)
            .map(res => res ? { exist: true } : null);
    }

    /**
     * 创建
     * 
     * 
     * @memberof DicKeyEditComponent
     */
    onCreate() {
        if (!this.hasError()) {
            this.bladeData.isLoading = true;
            let formValue = this.formGroup.value;
            this.currentEntity.stateGroupName = formValue.stateGroupName;
            this.matrixStateGroupService.create(this.currentEntity)
                .then(res => {
                    if (res) {
                        let parentContainerInstance = this.parentBlade.bladeContainerInstance;
                        parentContainerInstance.refresh(res);
                        this.bladeService.closeBlade(this.currentBlade);
                    }
                })
                .catch(error => this.bladeData.error = error)
                .then(res => this.bladeData.isLoading = false);
        }
    }

    saveChange(): any | Promise<any> | Observable<any> {
        if (this.canSaveChange(this.currentBlade)) {
            this.bladeData.isLoading = true;
            let formValue = this.formGroup.value;
            this.currentEntity.stateGroupName = formValue.stateGroupName;
            return this.matrixStateGroupService.update(this.currentEntity)
                .then(res => {
                    if (res) {
                        (this.parentBlade.bladeContainerInstance).refresh(res);
                        this.bladeService.closeBlade(this.currentBlade);
                        return true;
                    }
                })
                .catch(error => this.bladeData.error = error)
                .then(res => this.bladeData.isLoading = false);
        }
    }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/matrix-config/matrix-state-group-edit/matrix-state-group-edit.component.ts